package com.heeexy.example.util.aop;
import java.lang.reflect.Method;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.heeexy.example.entity.OperationLog;
import com.heeexy.example.service.OperationLogService;
import com.heeexy.example.service.TokenService;
import com.heeexy.example.util.IpUtil;
import com.heeexy.example.util.OperLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;


/**
 * 切面处理类，操作日志异常日志记录处理
 *
 * @author wu
 * @date 2019/03/21
 */
@Aspect
@Component
public class OperLogAspect {
    //    @Value("${version}")
//    private String operVer;
//    static Logger logger = LoggerFactory.getLogger(DataSourceAspect.class);
    @Autowired
    private OperationLogService operationLogService;
    @Autowired
    private TokenService tokenService;
    /**
     * 设置操作日志切入点 记录操作日志 在注解的位置切入代码
     */
    @Pointcut("@annotation(com.heeexy.example.util.OperLog)")
    public void operLogPoinCut() {

    }

    /**
     * 设置操作异常切入点记录异常日志 扫描所有controller包下操作
     */
    @Pointcut("execution(* com.heeexy.example.controller..*.*(..))")
    public void operExceptionLogPoinCut() {

    }

    /**
     * 正常返回通知，拦截用户操作日志，连接点正常执行完成后执行， 如果连接点抛出异常，则不会执行
     *
     * @param joinPoint 切入点
     * @param keys      返回结果
     */
    @AfterReturning(value = "operLogPoinCut()", returning = "keys")
    public void saveOperLog(JoinPoint joinPoint, Object keys) {
        // 获取RequestAttributes
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        // 从获取RequestAttributes中获取HttpServletRequest的信息
        HttpServletRequest request = (HttpServletRequest) requestAttributes
                .resolveReference(RequestAttributes.REFERENCE_REQUEST);

        OperationLog operlog = new OperationLog();
        try {
            // 从切面织入点处通过反射机制获取织入点处的方法
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            // 获取切入点所在的方法
            Method method = signature.getMethod();
            // 获取操作
            OperLog opLog = method.getAnnotation(OperLog.class);
            if (opLog != null) {
                String operDesc = opLog.operDesc();
                operlog.setTl_log(operDesc); // 操作描述
            }
            operlog.setRes_param(JSON.toJSONString(keys)); // 返回结果
            //operlog.setU_Id(this.getLoginUserId()); // 请求用户ID
            operlog.setTl_ip(IpUtil.getIpAddr(request)); // 请求IP
            operlog.setTl_time(new Date()); // 创建时间
            operationLogService.insert(operlog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
